package com.gamificationlibrary.fragments;


import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.gamificationlibrary.gamificationlibrary.R;
import com.gamificationlibrary.gamificationlibrary.leaderboard.Leaderboard;
import com.gamificationlibrary.gamificationlibrary.model.Player;
import com.gamificationlibrary.view.LeaderboardView;

public class SpinnerLeaderboardsFragment extends Fragment{

	private ArrayList<Leaderboard> leaderboards;
	private Player player;
	private String[] fieldSpinner;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
	{
        // Inflate the layout for this fragment
        final View rootView= inflater.inflate(R.layout.geoleaderboards_fragment, container, false);
        
    	Bundle bundle=getArguments();
		leaderboards = (ArrayList<Leaderboard>) bundle.get(LeaderboardView.EXTRAS_LEADERBOARD);
		player=(Player) bundle.getSerializable(LeaderboardView.EXTRAS_PLAYER);
		
        Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner_leaderboard);
    	List<String> list = new ArrayList<String>();
    	for(int i=0;i<leaderboards.size();i++)
    	{
    		if(this.fieldSpinner!=null)
    	    	list.add(fieldSpinner[i]);
    		else
    		{
    			list.add("Spinner"+i);
    		}
    	}
    	
    
    	ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
    		android.R.layout.simple_spinner_item, list);
    	dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinner.setAdapter(dataAdapter);
    	
    	spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			private LeaderboardView leaderboardView1;

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				leaderboardView1=(LeaderboardView) rootView.findViewById(R.id.leaderboardView_Leaderboard);
	   	    	leaderboardView1.putContent(leaderboards.get(position), player);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		return rootView; 
    }
	
	public void setFieldsSpinner(String[] fieldsNames)
	{
		this.fieldSpinner=fieldsNames;
	}
	
}
